<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <p @change="changeHandler">
        视图:
        <input type="checkbox" :checked="hobbys.includes('css')" value="css" />
        CSS
        <input
          type="checkbox"
          :checked="hobbys.includes('html')"
          value="html"
        />
        HTML
        <input type="checkbox" :checked="hobbys.includes('js')" value="js" /> JS
        <br />
        数据: {{hobbys}}
      </p>
    </div>
    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          hobbys: ["css", "html"],
        },
        methods: {
          changeHandler(e) {
            const target = e.target;
            const value = target.value;
            const checked = target.checked;
            if (checked === true) {
              this.hobbys.push(value);
            } else {
              const i = this.hobbys.findIndex((el) => el === value);
              this.hobbys.splice(i, 1);
            }
          },
        },
      });
    </script>
  </body>
</html>
